using System;
using System.Collections.Generic;
using System.Linq;
using RFS.BizLogic.PaiShi.Ports;
using RFS.BizLogic.utils;
using RFS.Domain.PaiShi;
using RFS.Domain.PaiShi.Accounting;
using RFS.Domain.PaiShi.WorkFlow;
using RFS.Domain.Sys;

namespace RFS.BizLogic.PaiShi.Impl
{
    public class WorkFlowManager : IWorkFlowManager
    {
        private IRepoPaiShi _repo;
        private ILog _log;

        public WorkFlowManager(IRepoPaiShi repo, ILog log, IConfigT config, IPriceManagerPort priceManagerPort)
        {
            _repo = repo;
            _log = log;
            _config = config;
            _priceManagerPort = priceManagerPort;
        }

        public IWorkFlowNodeWrapper GetByBatchNo(AccountPeriod bn,BizFlowNodeEnum node= BizFlowNodeEnum.JiaoYi)
        {

            var bd  =bn ;
            
            IWorkFlowNodeWrapper ww= new WorkFlowNodeWrapper(  )  ;
                
            // if (node == BizFlowNodeEnum.JiaoYi) ww= new WorkFlowNodeWrapper(createJiaoYi(null,bd) );
            if (node == BizFlowNodeEnum.YanShou) ww= new WorkFlowNodeWrapper(createYanShou(null,bd));
            
            return ww;

        }

        public IWorkFlowNodeWrapper GetByYanShouNode(AccountPeriod bn, bool fromProdLine = true, bool fromRegroup = false)
        {
            var bd  = bn ;
           return new WorkFlowNodeWrapper(createYanShou(null,bd, fromProdLine,fromRegroup ) );
        }

        public IWorkFlowNodeWrapper GetUnRecordedInAccYanShouNode(AccountPeriod period)
        {
             
            return new WorkFlowNodeWrapper(CreateUnRecordedYanShou(period, true,true  ) );
        }

        public IWorkFlowNodeWrapper GetZuhuoNode(AccountPeriod period,  AccountPeriod jyDate)
        {
            return new WorkFlowNodeWrapper(CreateZuhuoWf(period,jyDate));
        }

        public IWorkFlowNodeWrapper GetByFahuoNodde(AccountPeriod date,AccountPeriod jydate)
        {
            
            return new WorkFlowNodeWrapper(createFaHuo(null,date, jydate));

        }

        private WorkFlowNode CreateZuhuoWf(  AccountPeriod bd , AccountPeriod jyDate  )
        {
            
            // load the date of 
            try
            {
    
                var os=_repo.GetZuhuoOrderItemSet(   bd )
                    .Select(x=>x.OfOrderItem())
                     .ToList();

                FillThePrice(os, jyDate.BeginDate );

                return new WorkFlowNode(os, bd.BeginDate, bd.BeginDate, "组货集");

            }
            catch (Exception e)
            {
                _log.Err(e);
                return new WorkFlowNode(new List<IOrderItem>(),bd.BeginDate,bd.BeginDate, "组货集");

            }

            
            
        }

        private WorkFlowNode CreateUnRecordedYanShou(  AccountPeriod bd, bool fromPl, bool fromregroup)
        {
            // load the date of 
            try
            {
                
                /*
                 * 0-新增供货
1-流拍重组
2-投诉重组
3-？
4-？
5-？
6 流水线到货，
7-？
8 远程录入
                 */ 
                var os=_repo.GetUnRecordedInAccJsonDataSet(BizFlowNodeEnum.YanShou, bd)
                    .Where(x=>$"{x.供货来源 }".Contains("8")    
                    )
                    .Select(x=>1.OfYanShou_GHDataItem(x as object))
                    .ToList();

                FillThePrice(os,bd.BeginDate);
                return new WorkFlowNode(os, bd.BeginDate, bd.BeginDate, "拍市验收_未记账");

            }
            catch (Exception e)
            {
                _log.Err(e);
                return new WorkFlowNode(new List<IOrderItem>(),bd.BeginDate,bd.BeginDate,"拍市验收_未记账");

            }

            
            
        }


        private IPriceManagerPort _priceManagerPort;
        private IConfigT _config;

        void FillThePrice(IEnumerable<IOrderItem> os,DateTime date)
        {
            foreach (var o in os)
            {
                try
                {
                    var p = _priceManagerPort.GetPrice(date, o.Product,true );
                    if (p == null && _config.ConfigSys.CompareModeOfCash)
                        throw new Exception($"系统计价模式，但没有[{date}-{o.Product}]的价格");
                    o.Amount= o.Quantity * p.Price ;
                }
                catch (Exception e)
                {
                    _log.Err(e);
                }
               

            }

        }

        private WorkFlowNode createFaHuo(IWorkFlowNode parent, AccountPeriod bd,AccountPeriod jydate)
        {
           
            // load the date of 

            try
            {
              var  os = _repo.GetJsonDataSetFahuo( bd  )
                    .Select(x=>1.OfFaHuo_LuRuDataItem(x as object))
                    .ToList();
              
               FillThePrice(os,jydate.BeginDate);

                return new WorkFlowNode(os,bd.BeginDate,bd.EndDate,"基地发货");
            }
            catch (Exception e)
            {
                _log.Err(e);
                return new WorkFlowNode(new List<IOrderItem>(),bd.BeginDate,bd.EndDate,"基地发货");

            }
 
        }

        private WorkFlowNode createYanShou(IWorkFlowNode parent, AccountPeriod bd, bool fromProdLine = true,
            bool fromRegroup = false)
        {
            
        
            
            // load the date of 
            try
            {
                
                /*
                 * 0-新增供货
1-流拍重组
2-投诉重组
3-？
4-？
5-？
6 流水线到货，
7-？
8 远程录入
                 */ 
                var os=_repo.GetJsonDataSet(BizFlowNodeEnum.YanShou, bd.BeginDate,bd.EndDate )
                    .Where(x=>$"{x.供货来源 }".Contains("8")  
                              || ($"{x.供货来源 }".Contains("6") && fromProdLine  )
                              || ($"{x.供货来源 }".Contains("2") &&  fromRegroup   )
                              || ($"{x.供货来源 }".Contains("1") &&  fromRegroup   ) 
                              
                              
                              )
                    .Select(x=>1.OfYanShou_GHDataItem(x as object))
                    .ToList();

                FillThePrice(os,bd.BeginDate);


                return new WorkFlowNode(os, bd.BeginDate, bd.EndDate, "拍市验收");

            }
            catch (Exception e)
            {
                _log.Err(e);
                return new WorkFlowNode(new List<IOrderItem>(),bd.BeginDate,bd.EndDate,"基地发货");

            }
  
           
        }


        // private DateTime bNo= DateTime.Today;


        // WorkFlowNode createJiaoYi(IWorkFlowNode parent = null, DateTime bd=default)
        // {
        //     var n = new WorkFlowNode(bd,bd);
        //     n.ParentNode=parent;
        //     n.KeyName = "拍市交易";
        //     
        //
        //     return n;
        // }
                

        
        
    }
}